import { Component } from '@angular/core';
import {Events, ModalController, NavController, Platform} from 'ionic-angular';
import {AppGlobal, AppService} from "../../services/app.service";
import {UserInfoService} from "../../services/userInfo.service";
import {User} from "../../models/user-model";
import {JPush} from "@jiguang-ionic/jpush";
import {Storage} from "@ionic/storage";
import {MessageService} from "../../services/message.service";
import {ResDataModel} from "../../models/res-data-model";

/**
 * Generated class for the SettingPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
  selector: 'page-setting',
  templateUrl: 'setting.html',
})
export class SettingPage {

  userInfo:User;
  pushEnable:boolean = true;
  unReadMsgCount:number;
  //userName:string;
  //userCode:string;
  //nickName:string;
  //userInfoPage:any;
  constructor(public navCtrl: NavController,
              public modalCtrl: ModalController,
              private platform: Platform,
              private events: Events,
              private appService:AppService,
              private userInfoService:UserInfoService,
              private messageService:MessageService,
              private storage:Storage,
              private jPush:JPush) {
    console.log('Setting 构造函数！');
    this.listenEvents();
    this.userInfo = this.userInfoService.user;
    //从缓存中获取推送设置
    this.storage.get("pushEnable").then((value)=>{
      if (value == "0"){
        this.pushEnable = false;
      } else {
        this.pushEnable = true;
      }
    })
  }


//生命周期钩子
  ionViewDidLoad(){
    console.log('触发ionViewDidLoad');
  }

  ionViewWillEnter(){
    this.getUnReadMsgCount();
    console.log('触发ionViewWillEnter');
  }

  ionViewDidEnter(){
    console.log('触发ionViewDidEnter');
  }

  ionViewWillLeave(){
    console.log('触发ionViewWillLeave');
  }

  ionViewDidLeave(){
    console.log('触发ionViewDidLeave');
  }

  ionViewWillUnload(){
    console.log('触发ionViewWillUnload');
  }

  /**
   * 登出
   */
  logOut() {
    // let modal = this.modalCtrl.create(LoginPage);
    // modal.present();
    //this.viewCtrl.dismiss();
    this.appService.confirm('是否确定要退出登录？',
      ()=>{
        this.appService.httpPost('AppLoginCtrl_logout',{},null,(res:ResDataModel) => {
          //console.log("code:"+data.code+" msg:"+data.msg+" flag:"+data.data[1].value+" token:"+data.data[2].value);
          let flag = res.datas[0].value;
          if(flag == 'success') {//登出成功
            //清除token
            AppGlobal.token = '';
            this.storage.remove('token');
            //发布跳转登录页的事件
            this.events.publish('toLogin');
          } else {//登出失败
            this.appService.alert('登出失败');
          }
        },true);
      },
      ()=>{
        //不登出，啥也不干
      })

  }

  goPage(page){
    if(page != null){
      //this.navCtrl.push(page);
      let modal = this.modalCtrl.create(page);
      modal.present();
    }
  }

  /**
   * 设置推送开启
   * @param event
   */
  setPush(event){
    var pushValue:string;
    if(this.pushEnable){//开启推送
      pushValue = "1";
    } else{//关闭推送
      pushValue = "0";
    }

    if(this.platform.is('cordova')) {//native
      if(this.pushEnable) {//开启推送
        //先判断是否已经关闭
        this.jPush.isPushStopped().then((result)=>{
          if (result == 0) {
            // 已开启，无需再开启
          } else {
            // 已关闭，恢复服务
            this.jPush.resumePush();
          }
        })
      }else{
        this.jPush.stopPush();
      }
    }

    this.storage.set('pushEnable',pushValue);
    console.log(this.pushEnable);
  }

  /**
   * 监听事件
   */
  listenEvents() {
    this.events.subscribe('updateMsgCount',() => {
      console.log('更新显示消息数量');
      this.getUnReadMsgCount();
    })
  }

  /**
   * 获取未读消息数量
   */
  getUnReadMsgCount() {
    this.messageService.getUnReadMsgCount().then(res=>{
      console.log(res.rows.item(0).countNum);
      this.unReadMsgCount = res.rows.item(0).countNum;
    })
  }
}
